home *** CD-ROM | disk | FTP | other *** search
/ Nebula 1 / Nebula One.iso / Communications / pcomm / Source / x_menu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-06-12  |  7.4 KB  |  342 lines

  1. /*
  2.  * Open a window to display the choices of file transfer protocols and
  3.  * prompt for the file name(s).  A non-zero return code means turn the
  4.  * input routine back on.
  5.  */
  6.  
  7. #include <stdio.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <curses.h>
  11. #include "config.h"
  12. #include "extrnl.h"
  13. #include "misc.h"
  14. #include "xmodem.h"
  15.  
  16. int
  17. xfer_menu(up)
  18. int up;
  19. {
  20.     extern int fd;
  21.     extern char *null_ptr;
  22.     WINDOW *xm_win, *newwin();
  23.     char buf[2048], *list, *strcat(), *strcpy();
  24.     static char *get_names(), *get_extrnl();
  25.     int type, is_batch, i, ans, num_extrnl, n, ret_code;
  26.     void xfer_win(), xfer_ascii(), do_extrnl(), error_win();
  27.  
  28.     num_extrnl = (up) ? extrnl->up_entries : extrnl->dn_entries;
  29.     xm_win = newwin(14+num_extrnl, 20, 2, 45);
  30.  
  31.     mvwaddstr(xm_win, 2, 3, "1) xmodem");
  32.     mvwaddstr(xm_win, 3, 3, "2) xmodem-1k");
  33.     mvwaddstr(xm_win, 4, 3, "3) modem7");
  34.     mvwaddstr(xm_win, 5, 3, "4) ymodem");
  35.     mvwaddstr(xm_win, 6, 3, "5) ymodem-g");
  36.     mvwaddstr(xm_win, 7, 3, "6) ASCII");
  37.  
  38.     for (i=0; i<num_extrnl; i++)
  39.         mvwprintw(xm_win, i+8, 3, "%d) %-12.12s", i+7, extrnl->name[up][i]);
  40.     mvwaddstr(xm_win, i+8, 3, "E) (external)");
  41.     mvwaddstr(xm_win, i+10, 3, "<ESC> to Abort");
  42.     mvwaddstr(xm_win, i+11, 3, "Protocol:");
  43.     box(xm_win, VERT, HORZ);
  44.     if (up)
  45.         mvwattrstr(xm_win, 0, 6, A_BOLD, " Upload ");
  46.     else
  47.         mvwattrstr(xm_win, 0, 5, A_BOLD, " Download ");
  48.  
  49.     wmove(xm_win, i+11, 13);
  50.     wrefresh(xm_win);
  51.                     /* get the protocol */
  52.     type = -1;
  53.     while ((ans = wgetch(xm_win)) != ESC) {
  54.         switch (ans) {
  55.             case '1':
  56.                 type = XMODEM;
  57.                 break;
  58.             case '2':
  59.                 type = XMODEM_1k;
  60.                 break;
  61.             case '3':
  62.                 type = MODEM7;
  63.                 break;
  64.             case '4':
  65.                 type = YMODEM;
  66.                 break;
  67.             case '5':
  68.                 type = YMODEM_G;
  69.                 break;
  70.             case '6':
  71.                 type = XASCII;
  72.                 break;
  73.             case '7':
  74.                 if (num_extrnl >= 1)
  75.                     type = EXT_1;
  76.                 else
  77.                     beep();
  78.                 break;
  79.             case '8':
  80.                 if (num_extrnl >= 2)
  81.                     type = EXT_2;
  82.                 else
  83.                     beep();
  84.                 break;
  85.             case '9':
  86.                 if (num_extrnl >= 3)
  87.                     type = EXT_3;
  88.                 else
  89.                     beep();
  90.                 break;
  91.             case 'e':
  92.             case 'E':
  93.                 type = EXT_MANUAL;
  94.                 break;
  95.             default:
  96.                 beep();
  97.         }
  98.         if (type != -1)
  99.             break;
  100.     }
  101.     werase(xm_win);
  102.     wrefresh(xm_win);
  103.     delwin(xm_win);
  104.                     /* chicken'd out */
  105.     if (type == -1)
  106.         return(0);
  107.  
  108.     if (fd == -1) {
  109.         error_win(0, "Not currently connected to any host", "");
  110.         return(0);
  111.     }
  112.                     /* which protocol? */
  113.     ret_code = 0;
  114.     is_batch = 0;
  115.     switch(type) {
  116.         case MODEM7:
  117.         case YMODEM:
  118.         case YMODEM_G:        /* built-in protocols */
  119.             is_batch++;
  120.             /* fall thru */
  121.         case XMODEM:
  122.         case XMODEM_1k:        /* non-batch built-ins */
  123.             list = null_ptr;
  124.             /*
  125.              * When receiving in a batch mode, don't prompt
  126.              * for file names.
  127.              */
  128.             if (up || !is_batch) {
  129.                 if (!(list = get_names(up, type, is_batch)))
  130.                     break;
  131.             }
  132.             xfer_win(list, up, type);
  133.             ret_code++;
  134.             break;
  135.         case XASCII:        /* ascii xfer, yuck! */
  136.             if (list = get_names(up, type, FALSE)) {
  137.                 xfer_ascii(list, up);
  138.                 if (!up)
  139.                     ret_code++;
  140.             }
  141.             break;
  142.         case EXT_1:
  143.         case EXT_2:
  144.         case EXT_3:        /* one of the externals */
  145.             n = type -NUM_INTERNAL;
  146.             strcpy(buf, extrnl->command[up][n]);
  147.                     /* see if we need to prompt for files */
  148.             if (extrnl->prompt[up][n] == 'Y') {
  149.                 if (list = get_names(up, type, TRUE)) {
  150.                     strcat(buf, " ");
  151.                     strcat(buf, list);
  152.                 }
  153.                 else
  154.                     break;
  155.             }
  156.             do_extrnl(buf);
  157.             ret_code++;
  158.             break;
  159.         case EXT_MANUAL:    /* the manual external protocol */
  160.             if (list = get_extrnl(up)) {
  161.                 do_extrnl(list);
  162.                 ret_code++;
  163.             }
  164.             break;
  165.     }
  166.     return(ret_code);
  167. }
  168.  
  169. char *protocol[NUM_INTERNAL] = {"xmodem", "xmodem-1k", "modem7", "ymodem",
  170.     "ymodem-g", "ASCII"};
  171.  
  172. /*
  173.  * Prompt for a list of files for the transfer programs.  Since expand()
  174.  * is used, it returns a pointer to a static area.  Returns a NULL if 
  175.  * you chicken'd out.
  176.  */
  177.  
  178. static char *
  179. get_names(up, type, is_batch)
  180. int up, type, is_batch;
  181. {
  182.     int got_it;
  183.     WINDOW *gn_win, *newwin();
  184.     char *ans, *file, *list, buf[40], *expand(), *get_str(), *strtok();
  185.     void st_line();
  186.     struct stat stbuf;
  187.  
  188.     touchwin(stdscr);
  189.     refresh();
  190.     st_line("");
  191.  
  192.     gn_win = newwin(7, 70, 5, 5);
  193.     mvwaddstr(gn_win, 3, 4, "Enter filename: ");
  194.     box(gn_win, VERT, HORZ);
  195.     if (up) {
  196.         if (type < NUM_INTERNAL)
  197.             sprintf(buf, " Send %s ", protocol[type]);
  198.         else
  199.             sprintf(buf, " Send %s ", extrnl->name[up][type-NUM_INTERNAL]);
  200.     }
  201.     else {
  202.         if (type < NUM_INTERNAL)
  203.             sprintf(buf, " Receive %s ", protocol[type]);
  204.         else
  205.             sprintf(buf, " Receive %s ", extrnl->name[up][type-NUM_INTERNAL]);
  206.     }
  207.     mvwattrstr(gn_win, 0, 3, A_BOLD, buf);
  208.  
  209.     while (1) {
  210.         wmove(gn_win, 3, 20);
  211.         wrefresh(gn_win);
  212.                     /* get the answers */
  213.         if (is_batch)
  214.             ans = get_str(gn_win, 60, "", "\n");
  215.         else
  216.             ans = get_str(gn_win, 60, "", " \t\n");
  217.  
  218.         if (ans == NULL || *ans == '\0') {
  219.             list = NULL;
  220.             break;
  221.         }
  222.         list = expand(ans);
  223.                     /* batches are checked on-the-fly */
  224.         if (is_batch)
  225.             break;
  226.         /*
  227.          * Here we have the opportunity to determine the read and
  228.          * write permissions before things get started.  Much nicer
  229.          * than finding out later when there's no way to fix it.
  230.          * Only checks the first file.
  231.          */
  232.         file = strtok(list, " \t");
  233.                     /* sanity checking */
  234.         if (!stat(file, &stbuf)) {
  235.             if ((stbuf.st_mode & S_IFREG) != S_IFREG) {
  236.                 beep();
  237.                 clear_line(gn_win, 4, 15, TRUE);
  238.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "Not a regular file");
  239.                 wrefresh(gn_win);
  240.                 wait_key(gn_win, 3);
  241.                 clear_line(gn_win, 4, 15, TRUE);
  242.                 clear_line(gn_win, 3, 20, TRUE);
  243.                 continue;
  244.             }
  245.         }
  246.                     /* check read permission */
  247.         if (up) {
  248.             if (access(file, 0)) {
  249.                 beep();
  250.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "Can't find file");
  251.                 wrefresh(gn_win);
  252.                 wait_key(gn_win, 3);
  253.                 clear_line(gn_win, 4, 15, TRUE);
  254.                 clear_line(gn_win, 3, 20, TRUE);
  255.                 continue;
  256.             }
  257.             if (access(file, 4)) {
  258.                 beep();
  259.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "No read permission");
  260.                 wrefresh(gn_win);
  261.                 wait_key(gn_win, 3);
  262.                 clear_line(gn_win, 4, 15, TRUE);
  263.                 clear_line(gn_win, 3, 20, TRUE);
  264.                 continue;
  265.             }
  266.             break;
  267.         }
  268.                     /* check write permission */
  269.         got_it = 0;
  270.         switch(can_write(file)) {
  271.             case DENIED:
  272.                 beep();
  273.                 clear_line(gn_win, 4, 15, TRUE);
  274.                 mvwattrstr(gn_win, 4, 15, A_BOLD, "No write permission");
  275.                 wrefresh(gn_win);
  276.                 wait_key(gn_win, 3);
  277.                 clear_line(gn_win, 4, 15, TRUE);
  278.                 clear_line(gn_win, 3, 20, TRUE);
  279.                 break;
  280.             case OK_BUT_EXISTS:
  281.                 if (!yes_prompt(gn_win, 4, 15, A_BOLD, "File exists, overwrite")) {
  282.                     clear_line(gn_win, 4, 15, TRUE);
  283.                     clear_line(gn_win, 3, 20, TRUE);
  284.                     break;
  285.                 }
  286.                 /* fall thru */
  287.             case WRITE_OK:
  288.                 got_it++;
  289.                 break;
  290.         }
  291.         if (got_it)
  292.             break;
  293.     }
  294.     werase(gn_win);
  295.     wrefresh(gn_win);
  296.     delwin(gn_win);
  297.  
  298.     return(list);
  299. }
  300.  
  301. /*
  302.  * Prompt for the Unix command line to be used as an external file
  303.  * transfer program.  Since expand() is used, it returns a pointer to
  304.  * a static area.
  305.  */
  306.  
  307. static char *
  308. get_extrnl(up)
  309. int up;
  310. {
  311.     WINDOW *ge_win, *newwin();
  312.     char *ans, *cmd, *get_str(), *expand();
  313.     void st_line();
  314.  
  315.     touchwin(stdscr);
  316.     refresh();
  317.     st_line("");
  318.                     /* prompt for command line */
  319.     ge_win = newwin(7, 70, 5, 5);
  320.     mvwaddstr(ge_win, 3, 4, "Enter Unix command: ");
  321.     box(ge_win, VERT, HORZ);
  322.  
  323.     if (up)
  324.         mvwattrstr(ge_win, 0, 3, A_BOLD, " Send (external) ");
  325.     else
  326.         mvwattrstr(ge_win, 0, 3, A_BOLD, " Receive (external) ");
  327.  
  328.     wmove(ge_win, 3, 24);
  329.     wrefresh(ge_win);
  330.                     /* get the line */
  331.     ans = get_str(ge_win, 60, "", "\n");
  332.     if (ans == NULL || *ans == '\0')
  333.         cmd = NULL;
  334.     else
  335.         cmd = expand(ans);
  336.  
  337.     werase(ge_win);
  338.     wrefresh(ge_win);
  339.     delwin(ge_win);
  340.     return(cmd);
  341. }
  342.